﻿using System;
using System.Collections.Generic;
using System.Collections;
using System.Linq;
using System.Text;
using System.IO;
using Microsoft.Office.Interop.Word;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Windows.Forms;
using Application = Microsoft.Office.Interop.Word.Application;

namespace Lottery
{
    public class InfoBox
    {
        public String Name;
        public String Depart;
        public String ImagePath;
    }

    public static class WordPublishObject
    {
        public enum PublishMethod { Word, Html };
        private static Queue<InfoBox> InfoBoxList = new Queue<InfoBox>();

        public static void Add(String Name, String Major, String ImagePath)
        {
            InfoBox box = new InfoBox();
            box.Name = Name;
            box.Depart = Major;
            box.ImagePath = ImagePath;
            InfoBoxList.Enqueue(box);
        }

        private static void PublishToHtml(String _FileName, String _Title)
        {
            DirectoryInfo di = new DirectoryInfo(_FileName);
            if (!di.Exists)
                di.Create();

            List<InfoBox> list = new List<InfoBox>();
            while (InfoBoxList.Count > 0)
            {
                list.Add(InfoBoxList.Dequeue());
            }

            StreamWriter Writer = new StreamWriter(_FileName + @"\index.htm", true, Encoding.UTF8);
            Writer.Write("<body>\n");
            Writer.Write("<table cellpadding=\"20\" align=\"center\">\n");
            Writer.Write("<tr>\n");
            Writer.Write("<th colspan=\"3\">\n");
            Writer.Write(_Title);
            Writer.Write("</th>\n");
            Writer.Write("</tr>\n");

            Writer.Write("<tr>\n");
            Writer.Write("<td>姓名</td>\n");
            Writer.Write("<td>部门</td>\n");
            Writer.Write("<td>照片</td>\n");
            Writer.Write("</tr>\n");

            foreach (InfoBox box in list)
            {
                Writer.Write("<tr>\n");

                Writer.Write("<td>");
                Writer.Write(box.Name);
                Writer.Write("</td>\n");

                Writer.Write("<td>");
                Writer.Write(box.Depart);
                Writer.Write("</td>\n");

                Writer.Write("<td>");
                FileInfo fout = new FileInfo(box.ImagePath);
                FileInfo fin = new FileInfo(_FileName + @"\" + fout.Name);
                if (!fin.Exists)
                {
                    fout.CopyTo(fin.FullName);
                }
                Writer.Write("<img src=\"" + fout.Name + "\">\n");
                Writer.Write("</td>\n");

                Writer.Write("</tr>\n");
            }
            Writer.Write("</table>\n");
            Writer.Write("</body>\n");
            Writer.Close();
        }
        private static void PublishToWord(String _FileName, String _Title)
        {
            Object Miss = System.Type.Missing;
            Object FileName = _FileName;
            Object LinkToFile = System.Type.Missing;
            Object SaveWithDocument = true;
            Object FileFormat = WdSaveFormat.wdFormatDocument;

            _Document Doc = null;
            _Application App = null;

            App = new Application();
            App.Visible = false;

            FileInfo fi = new FileInfo(_FileName);
            if (fi.Exists)
            {
                Doc = App.Documents.Open(ref FileName, ref Miss, ref Miss, ref Miss, ref Miss,
                ref Miss, ref Miss, ref Miss, ref Miss, ref Miss, ref Miss, ref Miss, ref Miss,
                ref Miss, ref Miss, ref Miss);
            }
            else
            {
                Doc = App.Documents.Add(ref Miss, ref Miss, ref Miss, ref Miss);
            }

            List<InfoBox> list = new List<InfoBox>();
            while (InfoBoxList.Count > 0)
            {
                list.Add(InfoBoxList.Dequeue());
            }

            Object Start = 0;
            Object End = 0;
            Range tableLocation = Doc.Range(ref Start, ref End);
            Table table = Doc.Tables.Add(tableLocation, list.Count + 2, 3, ref Miss, ref Miss);

            table.Range.Bold = 1;
            table.Range.Font.Size = 36;

            table.Cell(1, 1).Range.Text = _Title;
            table.Cell(1, 2).Merge(table.Cell(1, 1));
            table.Cell(1, 2).Merge(table.Cell(1, 1));

            table.Cell(2, 1).Range.Text = "姓名";
            table.Cell(2, 2).Range.Text = "部门";
            table.Cell(2, 3).Range.Text = "照片";

            int i = 2;
            foreach (InfoBox box in list)
            {
                i++;
                table.Cell(i, 1).Range.Text = box.Name;
                table.Cell(i, 2).Range.Text = box.Depart;
                table.Cell(i, 3).Range.InlineShapes.AddPicture(box.ImagePath, ref LinkToFile, ref SaveWithDocument, ref Miss);
            }

            Doc.SaveAs(ref FileName, ref FileFormat, ref Miss, ref Miss, ref Miss
                , ref Miss, ref Miss, ref Miss, ref Miss, ref Miss
                , ref Miss, ref Miss, ref Miss, ref Miss, ref Miss, ref Miss);

            App.Application.Quit(ref Miss, ref Miss, ref Miss);
        }

        public static void Publish(String _FileName, String _Title, PublishMethod _Method)
        {
            if (_Method == PublishMethod.Word)
            {
                PublishToWord(_FileName, _Title);
            }
            else if(_Method == PublishMethod.Html)
            {
                PublishToHtml(_FileName, _Title);
            }
        }
    }
}
